LifeExpIncomFinal <- "https://raw.githubusercontent.com/APM3030/STA553/main/homework3/LifeExpIncomFinal.csv"
LifeExpIncomFinal <- read_csv(LifeExpIncomFinal)
## New names:
## * `` -> ...1
## * ...1 -> ...2
## Rows: 42486 Columns: 8
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): geo, region
## dbl (6): ...1, ...2, Year, Income, LifeExp, Population
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
LifeExpIncomFinal <- mutate(LifeExpIncomFinal, Year = as.integer(LifeExpIncomFinal$Year))
#subet data for year 2015
leifinal2015 <- LifeExpIncomFinal %>% filter(Year == 2015)
#plot the data 
#consider using ggplotly
plot_ly(
  data = leifinal2015,
  x = ~Income ,  # Horizontal axis 
  y = ~LifeExp ,   # Vertical axis   # must be a numeric factor
  text = ~geo,   #location in the hover text
hovertemplate = paste(
"<b>{text}</b><br><br>",
      "%{yaxis.title.text}: %{y:}<br>",

      "%{xaxis.title.text}: %{x:}<br>",

      "Population: %{marker.size:}",
      
      "<extra></extra>"),

  color = ~factor(geo),
  alpha  = 0.5 ,
  size = ~Population,
  type = "scatter",
  mode = "markers"
   ) %>%
    layout(showlegend = FALSE, 
            title =list(text = "Relationship Between Life Expectancy and GDP", 
                          font = list(family = "Arial",    
                                        size = 18,
                                      face = "bold")),
           xaxis = list( 
                    title=list(text = 'GDP Per Capita')),
           yaxis = list (
                    title = list(text = 'Life Expectancy')
           )

    )
## Warning: Ignoring 6 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
plot_ly(
    data = leifinal2015,
    x = ~Income,  # Horizontal axis 
    y = ~LifeExp,   # Vertical axis 
    color = ~factor(geo),  # must be a numeric factor
     text = ~Population,     # show the species in the hover text
     ## using the following hovertemplate() to add the information of the
     ## two numerical variable to the hover text.
     hovertemplate = paste('<i><b>Life Expectancy<b></i>: %{y}',
                           '<br><b>GDP Per Capita</b>:  %{x}',
                           '<br><b>%{text}</b>'),
     alpha  = 0.9,
     size = ~Population,
     type = "scatter",
     mode = "markers")
## Warning: Ignoring 6 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors